
<template>
  <div>
    <web-view
      @message="messageCb"
      fullscreen
      src="/static/html/index.html" />
  </div>
</template>

<script setup>
import { onLoad, onReady, onShow } from '@dcloudio/uni-app'
import { ref, onMounted, onBeforeUnmount, reactive  } from 'vue'
import mitt from 'mitt';
import { uploadImage } from '@/api/login'
const emitter = mitt();

const paramsData = reactive({data: {}}) // 路径参数
// 订阅事件
emitter.on('event', (e) => {
    console.log(' - - - > emitter ', e);
});

onLoad((options) => {
  console.log(' - - - >  load', options)
  if (options.mobile) {
    paramsData.data = options
  }
})

// #ifdef H5 || WEB
onShow(() => {
 
})
onLoad(() => {
  window.addEventListener('message', (event) => {
      if (event.data.action === 'updateImage') {
        console.log('-> message', event.data.image);
        if (event.data.image) {
          putbaseData(event.data.image)
        }
      }
  });
})

// 获取
const putbaseData = (image) => {
  uni.showLoading({
    title: 'loading'
  });
  let params = {
    mobile: paramsData.data.mobile || '',
    order: paramsData.data.order || '',
    image: image || ''
  }
  // console.log('params - 提交数据:', params);
  uploadImage(params).then(res => {
    if (res && res.code == 0) {
      uni.showToast({
        title: res.msg || 'upload success',
        icon: 'success',
        duration: 3000
      });
      uni.navigateTo({
        url:`/pages/result/index`,
      });
    } else {
      uni.showToast({
        title: res.msg,
        icon: 'none',
        duration: 3000
      });
    }
    uni.hideLoading();
  }).catch(err => {
    setTimeout(function () {
      uni.hideLoading();
    }, 3000);
    console.log('err :', err)
    uni.showToast({
      title: err.msg,
      icon: 'none',
      duration: 3000
    });
  })
}
// #endif

</script>
